package com.example.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.common.core.enums.ResultCode;
import com.example.common.core.utils.BCryptUtils;
import com.example.common.security.exception.ServiceException;
import com.example.common.security.service.TokenService;
import com.example.system.domain.teacher.Teacher;
import com.example.system.domain.teacher.dto.TeacherQueryDTO;
import com.example.system.domain.teacher.dto.TeacherSaveDTO;
import com.example.system.domain.teacher.vo.TeacherDetailVO;
import com.example.system.domain.teacher.vo.TeacherSelectVO;
import com.example.system.domain.teacher.vo.TeacherVO;
import com.example.system.mapper.teacher.TeacherMapper;
import com.example.system.service.ITeacherService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true)
public class TeacherServiceImpl implements ITeacherService {
    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    public TokenService tokenService;

    @Value("${jwt.secret}")
    private String secret;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int add(TeacherSaveDTO teacherSaveDTO) {
        List<Teacher> teacherList = teacherMapper.selectList(new LambdaQueryWrapper<Teacher>()
                .eq(Teacher::getIdentificationCard, teacherSaveDTO.getIdentificationCard()));
        if(CollectionUtil.isNotEmpty(teacherList)){
            throw new ServiceException(ResultCode.AILED_USER_EXISTS);
        }
        Teacher teacher = new Teacher();
        teacher.setNickName(teacherSaveDTO.getNickName());
        teacher.setTitle(teacherSaveDTO.getTitle());
        teacher.setEmail(teacherSaveDTO.getEmail());
        teacher.setPhone(teacherSaveDTO.getPhone());
        teacher.setIdentificationCard(teacherSaveDTO.getIdentificationCard());
        teacher.setHireDate(teacherSaveDTO.getHireDate());
        teacher.setBirthday(teacherSaveDTO.getBirthday());
        teacher.setAddress(teacherSaveDTO.getAddress());
        teacher.setSex(teacherSaveDTO.getSex());
        teacher.setPassword(BCryptUtils.encryptPassword(teacherSaveDTO.getPassword()));
        teacher.setBio(teacherSaveDTO.getBio());
        return teacherMapper.insert(teacher);
    }

    @Override
    public List<TeacherVO> list(TeacherQueryDTO teacherQueryDTO) {
        PageHelper.startPage(teacherQueryDTO.getPageNum(), teacherQueryDTO.getPageSize());
        return teacherMapper.selectTeacherList(teacherQueryDTO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(Long teacherId) {
        Teacher teacher = teacherMapper.selectOne(new LambdaQueryWrapper<Teacher>()
                .eq(Teacher::getTeacherId, teacherId));
        if(teacher == null){
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
        // 在数据库中进行删除，后续在考虑引入缓存(redis、es、RabbitMQ)
        return teacherMapper.deleteById(teacherId);
    }

    @Override
    public TeacherDetailVO detail(Long teacherId) {
        Teacher teacher = teacherMapper.selectById(teacherId);
        if(teacher == null){
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
        TeacherDetailVO teacherDetailVO = new TeacherDetailVO();
        BeanUtil.copyProperties(teacher,teacherDetailVO);
        return teacherDetailVO;
    }


    // 只编辑基本信息，不编辑头像
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int edit(TeacherSaveDTO teacherEditDTO) {
        Teacher oldTeacher = teacherMapper.selectById(teacherEditDTO.getTeacherId());
        if(oldTeacher == null){
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
        oldTeacher.setNickName(teacherEditDTO.getNickName());
        oldTeacher.setEmail(teacherEditDTO.getEmail());
        oldTeacher.setPhone(teacherEditDTO.getPhone());
        oldTeacher.setIdentificationCard(teacherEditDTO.getIdentificationCard());
        oldTeacher.setHireDate(teacherEditDTO.getHireDate());
        oldTeacher.setBirthday(teacherEditDTO.getBirthday());
        oldTeacher.setAddress(teacherEditDTO.getAddress());
        oldTeacher.setSex(teacherEditDTO.getSex());
        oldTeacher.setPassword(BCryptUtils.encryptPassword(teacherEditDTO.getPassword()));
        oldTeacher.setTitle(teacherEditDTO.getTitle());
        oldTeacher.setBio(teacherEditDTO.getBio());
        return teacherMapper.updateById(oldTeacher);
    }

    @Override
    public List<TeacherSelectVO> selectList() {
        List<Teacher> teacherList = teacherMapper.selectList(new LambdaQueryWrapper<>());
        List<TeacherSelectVO> teacherSelectVOS = BeanUtil.copyToList(teacherList, TeacherSelectVO.class);
        return teacherSelectVOS;
    }
}
